var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
var READ_CONST_EVERY_ARTICLE_SWIPE_TIMES = 20;
var READ_CONST_START_FIND_AWARD = 2;
var read = {
  go_page_from_home() {
    var logger = utils.create_logger("read go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["视频", "小说"], true)) {
      logger();
      var ocr_text = ocr_utils.screen_exists_text_one("百度", true);
      if (ocr_text) {
        // utils.click_ocr_text(ocr_text);//这样写有问题，问题有待研究，先改成坐标形式
        utils.click_point.apply(
          utils,
          utils.get_size_point(point_map["首页百度"])
        );
        logger("找到--百度--");
        sleep(utils.get_random_interval(2, 0, 1, 1000));
      } else {
        logger("没找到--百度--");
      }
      // if (utils.click_text_starts_with("推荐")) {
      //   logger("找到--推荐--");
      //   sleep(utils.get_random_interval(2, 0, 1, 1000));
      // } else {
      //   logger("没找到--推荐--");
      // }
      this.init();
    } else {
      logger("NOT FOUND");
      this.after();
    }
  },
  init() {
    var content = ["小时前", "分钟前", "天前", "周前", "月前"];
    var ocr_text_results = ocr_utils.find_text_all(content, false);
    var length = ocr_text_results.length;
    if (length > 0) {
      console.log(
        "read init: 找到--" + length + "个--" + content.join("|||") + "--"
      );
      var times = READ_CONST_EVERY_ARTICLE_SWIPE_TIMES;
      for (var i = 0; i < length; i++) {
        if (i == 1) {
          break;
        }
        var { result } = ocr_text_results[i];
        this.run(i, result, times);
      }
      this.after();
    } else {
      console.log("read init: 没找到--" + content.join("|||") + "--");
      this.after();
    }
  },
  run(i, result, times) {
    var logger = utils.create_logger("read run: ");
    logger(
      "开始阅读第--" +
        (i + 1) +
        "--篇文章或视频,找到的是--" +
        result.text +
        "--"
    );
    utils.click_ocr_text(result);
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    if (utils.exists_text_starts_with("全屏")) {
      logger("找到--全屏--");
      logger("所以找到的是--视频--");
      var interval = utils.get_random_interval(30, 0, 5, 1);
      logger("将看视频--" + interval + "--秒");
      sleep(interval * 1000);
    } else {
      logger("没找到--全屏--");
      logger("所以找到的是--文章--");
      var swipe_times = times + random(0, 5);
      var has_swipe = 0;
      this.has_find_award = false;
      logger("将滑动--" + swipe_times + "--次");
      while (swipe_times > 0) {
        swipe(
          (device.width / 2) * utils.get_swipe_times(92, 8),
          (device.height / 2) * utils.get_swipe_times(92, 8),
          (device.width / 2) * utils.get_swipe_times(92, 8),
          (device.height / 2 - (device.height / 10) * 4) *
            utils.get_swipe_times(92, 8),
          utils.get_random_interval(5, 0, 3, 100)
        );
        swipe_times = swipe_times - 1;
        has_swipe = has_swipe + 1;
        sleep(utils.get_random_interval(2, 0, 3, 1000));
        logger("已滑动--" + has_swipe + "--次");
        if (
          has_swipe > READ_CONST_START_FIND_AWARD &&
          this.award_times < 5 &&
          !this.has_find_award
        ) {
          logger(
            "本篇文章已经滑动--" + has_swipe + "--次，开始检测是否有金币奖励"
          );
          this.find_award(false);
        }
        var is_over_contents = ["立即评论", "没有更多"];
        var ocr_text = ocr_utils.screen_exists_text_one(
          is_over_contents,
          false
        );
        if (ocr_text) {
          logger(
            "找到--" +
              is_over_contents.join(" 或 ") +
              "--，判断已经看完文章，即将返回"
          );
          sleep(utils.get_random_interval(1, 0, 1, 1000));
          break;
        }
      }
    }
    back();
    sleep(utils.get_random_interval(2, 0, 1, 1000));
  },
  award_times: 0,
  has_find_award: false,
  find_award(log_ocr_text) {
    var logger = utils.create_logger("read find_award: ");
    var ocr_text = ocr_utils.screen_exists_text_one("立即提现", log_ocr_text);
    if (ocr_text) {
      logger("找到--立即提现--");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      this.award_times = this.award_times + 1;
      this.has_find_award = true;
      if (utils.click_text_starts_with("看广告再")) {
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        logger("找到--看广告再--");
        ad.has_watch_ad_times = 0;
        ad.max_ad_times = 1000;
        ad.init(null, function () {
          var in_task_contents = ["立即收下", "开心收下", "关注"];
          for (var i = 0; i < in_task_contents.length; i++) {
            var in_task_content = in_task_contents[i];
            if (utils.exists_text_starts_with(in_task_content)) {
              logger("找到--" + in_task_content + "--");
              return true;
            }
          }
          return false;
        });
      } else {
        logger("没找到--看广告再--");
      }
    } else {
      logger("没找到--立即提现--");
    }
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};
module.exports = read;
